package com.qf.fronted.dao.impl;

import com.qf.fronted.dao.CartDao;
import com.qf.utils.DbUtils;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CartDaoImpl implements CartDao {

    QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    // 获取对应用户的购物车数据
    @Override
    public Map<String, Integer> getCartInfoOfUser(Integer userId) throws SQLException {
        Map<String, Integer> map = new HashMap<>();
        String sql = "SELECT good_id, num from cart WHERE u_id = ?";

        Connection connection = DbUtils.getConnection();
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, userId);
            ResultSet rs = ps.executeQuery();

            while(rs.next()) {
                map.put(rs.getString("good_id"), rs.getInt("num"));
            }

            DbUtils.colse(rs, ps, connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     *  插入购物车数据
     *
     * @param goodId   商品id
     * @param num      加入购物车的商品数量
     * @param userId   用户id
     */
    public void insertShoppingCartData(Integer goodId, Integer num, Integer userId, Connection connection) throws SQLException {
        String insertSql = "insert into cart(good_id, u_id, num, create_time) values(?, ?, ?, ?)";
        queryRunner.update(insertSql, goodId, userId,num, new Date());
    }

    /**
     * 更新数据库中购物车的数据
     * @param goodId  商品id
     * @param num     商品的数量
     * @param userId  用户id
     */
    public void updateShoppingCartData(Integer goodId, Integer num, Integer userId, Connection connection) throws SQLException {
        String updateSql = "update cart set num = ?, update_time = ? where good_id = ? and u_id = ?";

        queryRunner.update(updateSql, num, new Date(), goodId, userId);
    }

    @Override
    public void deleteCartData(Integer userId, Integer goodId) throws SQLException {
        String deleteSql = "delete from cart where good_id = ? and u_id = ?";

        queryRunner.update(deleteSql, goodId, userId);
    }

    @Override
    public Integer getNumOfSpecifyGood(Integer goodId, Integer userId) throws SQLException {
        String sql = "select num from cart where good_id = ? and u_id = ?";

        Integer num = (Integer) queryRunner.query(sql, new ScalarHandler<>(), goodId, userId);
        return num;
    }

    @Override
    public void deleteShoppingCartDataOfUser(Integer userId, Connection connection) throws SQLException {
        String deleteSql = "delete from cart where u_id = ?";

        queryRunner.update(deleteSql, userId);
    }


}
